<?php
/**
 * Created by PhpStorm.
 * User: woddp
 * Date: 2016/8/18
 * Time: 14:42
 */

namespace App\Models;

class SmsWlist extends Base {
    public function initialize() {
        parent::initialize();
    }

    public function getSource() {
        return "sms_wlist";
    }

    /**
     * @return string 返回当前表名
     */
    private function t() {
        return __CLASS__;
    }

    /**
     * @param $mobile 接口积分数据查询数据
     * @return mixed
     */
    public function search($mobile) {
        $query = $this->select('*')->from($this->t());
        if(!empty($mobile)) {
            $query->andWhere('mobile=:mobile:', ['mobile' => $mobile]);
        }
        try {
            $ret = $query->getQuery()->execute(); //执行语句
            $ret = $ret->toArray();
            //如果是数据 或者长度大于0
            if(is_array($ret) && count($ret) > 0) {
                return $ret;//返回查询到的数据 和状态
            }
        } catch(Exception $ex) {  //捕获错误
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;  //数据不存在
    }

    /**
     * 添加白名单号码
     *
     * @param array $mobiles 白名单号码数组
     *
     * @return int|false
     */
    public function add($mobiles) {
        $query = "REPLACE INTO `sms_wlist`(mobile) VALUES ('" . implode("'),('", $mobiles) . "')";
        try {
            if($this->db()->execute($query) !== false) {
                return true;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('REPLACE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**条数
     * @param $mobile  积分手机号码  根据手机号码查询
     * @return bool
     */
    public function total($mobile) {
        $query  = $this->select('count(*) as cnt')->from($this->t());
        $mobile = $mobile['mobile'];
        //如果手机号码不为空，就加上条件
        if(!empty($mobile)) {
            $query->andWhere('mobile=:mobile:', ['mobile' => $mobile]);
        }
        try {
            $ret = $query->getQuery()->execute();
            if($ret != false) {
                //转换成数组
                $ret = $ret->toArray();
                if(is_array($ret) && count($ret) > 0) {
                    return intval($ret[0]['cnt']);
                }
            }
        } catch(Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 获取短信信息
     *
     * @param int    $offset 起始位置
     * @param int    $limit 限制条数
     * @param array  $mobile 搜索得手机号码
     *
     * @return array|false
     */
    public function gets($mobile, $offset, $limit) {
        $query = $this->select(['mobile','integral'])->from($this->t());

        if(!empty($mobile)) {
            $query->andWhere('mobile=:mobile:', ['mobile' => $mobile]);
        }
        try {
            $ret = $query->orderBy('id DESC')->limit($limit, $offset)->getQuery()->execute();
            if($ret !== false) {
                return $ret->toArray();
            }
        } catch(Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**删除某个白名单号码
     * @param $mobile  白名单号码
     * @return bool
     */
    public function remove($mobile){

        try{
            return $this->db()->delete($this->getSource(),sprintf("mobile='%s' LIMIT 1",$mobile));

        }catch(\PDOException $ex){
            $this->errorLog('DELETE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**批量删除白名单号码
     * @param $mobiles
     * @return bool
     */
    public function dels($mobiles){
        $query = "delete from `sms_wlist` WHERE `mobile` IN(' ".implode(" ','" ,$mobiles)." ') ";
         try{
             if($this->db()->execute($query)!==false){
                 return true;
             }
         }catch(\PDOException $ex){
             $this->errorLog('REPLACE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
         }
    }


    /**
     * 批量更新;
     * 先清空表;然后新增
     *
     * @param array $mobiles 白名单号码数组
     *
     * @return bool
     */
    public function bulkUpdate($mobiles) {
        $query = "TRUNCATE TABLE `sms_wlist`;INSERT INTO `sms_wlist`(mobile) VALUES ('" . implode("'),('", $mobiles)."')";
        try {
            if($this->db()->execute($query) !== false) {
                return true;
            }
        } catch(\PDOException $ex) {
            $this->errorLog('REPLACE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

}